var cur_uid,cur_state,cur_page = 1,cur_ddid; 
var state_type = [];
var is_first = true;
var nomore = false;
var res = [];
var paytype = 1,w = null,pays = {},url = "",ptype = '',orderid;
var app = angular.module("myorders",[]);
app.controller("myorderscontroller",function($scope,$http){
	$scope.initView = function(){ 
		if(nomore){return;}	
		plus.nativeUI.showWaiting('加载中...'); 
		$http({ 
			method:'post',
			url:apiRoot,
			data:{
				action:"Orderid.orderidQuery",
				id:cur_uid,
				state:cur_state,
				page:cur_page 
			}
		}).then(function success(result){
			plus.nativeUI.closeWaiting(); 
			if(result.data.data == undefined){
				if(cur_page == 1){
					$scope.info = [];
				}				
				$scope.not_more  = 1;
				nomore = true; 
				return;  
			}
			res.push(result.data.data);
//			paytype = result.data.data[0].channel;
			console.log(JSON.stringify(result.data.data));
			$scope.info = res;
			cur_ddid = res[0][0].ddid; 
			cur_page++;
		},function errorFunction(e){       
			plus.nativeUI.closeWaiting(); 
			console.log(JSON.stringify(e));  
		}) 
		
		$http({
			method:'post',
			url:apiRoot,
			data:{
				action:'Orderid.checkOrderState',
				ddid:cur_ddid
			}
		}).then(function successFn(reponse){
			$scope.result = reponse.data.data;
			console.log(JSON.stringify($scope.result)); 
		},function(e){
			console.log(JSON.stringify(e));
		}) 
			
	} 
	
	$scope.ordershen = function($id,$state){
		console.log($id+'*****'+$state);
		$http({ 
			method:'post', 
			url:apiRoot,
			data:{
				action:"Orderid.orderidstate",
				id:$id,
				state:$state
			},
		}).then(function successCallback(data){
			if(data.data.error==0){
			  	mui.toast('提交成功'); 
			  	cur_page = (cur_page > 1) ? (cur_page - 1) : 1;
			  	console.log(cur_page);
			  	$scope.initView();
//				window.location.reload();
				console.log(JSON.stringify(data)); 
			}else{
			  	mui.toast('提交失败'); 
			}
	  },function errorfunction(e){
			plus.nativeUI.toast('提交失败');
		})
	}
	
	//评论
	$scope.pinglun=function($id){
		openPage('evaluate_for_goods.html',{ddid:$id})
	}
	//付款
//	$scope.pay=function($id,$num,$type,$ddid){
//		openPage('confirm_order.html',{goodId:$id,goodsNum:$num,pay_type:$type,orderId:$ddid});
//	}
	
	$scope.evaluate = function(ddid){
		openPage('evaluate_for_goods.html',{ddid:ddid});
	} 
	
	$scope.openOrderDetail = function($ddid,$state,$status){
		openPage('order_detail_for_evaluate.html',{orderId:$ddid,cur_state:$state,cur_status:$status});
	}
	
	/*调起支付*/
$scope.canpay=function(money,orderid,trade_name) {
//	alert(money+"#####"+orderid+"#####"+trade_name);
	if(w) {
		return;
	} //检查是否请求订单中
	if(paytype === 0) {
		ptype = 'alipay';
		url = webRoot + '/pay/Alipay.php?orderid=' + orderid + '&price=' + money;
	} else if(paytype === 1) {
		ptype = 'wxpay';
		url = webRoot + '/pay/index.php?price=' + money + '&orderid=' + orderid + '&trade_name=' + trade_name;
	}

	w = plus.nativeUI.showWaiting('支付中...', {
		width: '80px',
		height: '80px',
		background: 'rgba(0,0,0,0.3)'
	});
	// 请求支付订单
	var xhr = new XMLHttpRequest();
	xhr.onreadystatechange = function() {
		switch(xhr.readyState) {
			case 4:
				w.close();
				w = null;
				if(xhr.status == 200) {
					var order = xhr.responseText;
//					alert(order);
					plus.payment.request(pays[ptype], order, function(result) {
						toast('支付成功');
						//setSmallchange();/*保存记录，余额不变*/
					}, function(e){
						console.log("[" + e.code + "]：" + e.message);
					});
				} else {
					plus.nativeUI.toast("获取订单信息失败！");
				}
				break;
			default:
				break;
		}
	}
	xhr.open('get', url);
	xhr.send(); 
}
	
})


document.addEventListener("plusready",function(){
	var ws=plus.webview.currentWebview();
	title = ws.title;
	$('.mui-title').html(title);
	appElement = document.querySelector('[ng-controller=myorderscontroller]');
	$scope= angular.element(appElement).scope();
	cur_uid = plus.storage.getItem('uid');
	state_type = ['all', '6', '0', '1', '2']; //定义tab切换类型  
	//第一次执行all
	if(is_first){
		cur_state = 'all'; 
		$scope.initView(); 
		$scope.$apply(); 
		is_first = false; 
	}
	
	$('.mui-segmented-control .mui-control-item').on('tap', function() {
		var i = $(this).index();
		res = [];
		$scope.not_more = 0;
		nomore = false;
		cur_page = 1;
		cur_state = state_type[i];
		$scope.initView();
//		$scope.$apply();
		$('#content_box').css({ 'height': ($('.mui-control-content').eq(i).innerHeight() + 42) + 'px' });
		$('#sliderProgressBar').animate({ 'left': ($(this).index() * $(this).innerWidth() + 'px') }, 'fast');  
	})
	
		// 获取支付通道
	plus.payment.getChannels(function(channels) {
		for(var i in channels) {
			var channel = channels[i];
			pays[channel.id] = channel;
			checkServices(channel);
		}
	}, function(e) {
		toast("获取支付通道失败：" + e.message);
		outLine("获取支付通道失败：" + e.message);
	});

	// 检测是否安装支付服务 
	function checkServices(pc){
		if(!pc.serviceReady) {
			var txt = null;
			switch(pc.id) {
//				case "alipay":
//					txt = "检测到系统未安装“支付宝快捷支付服务”，无法完成支付操作，是否立即安装？";
//					break;
				default:
					txt = "系统未安装“" + pc.description + "”服务，无法完成支付，是否立即安装？";
					break;
			}
			plus.nativeUI.confirm(txt, function(e) {
				if(e.index == 0) {
					pc.installService();
				}
			}, pc.description);
		}
	}
 
}) 